package com.javasee.common.module;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.util.StringUtils;

/**
 * Project Name:
 * File Name   :
 * Package Name:
 *
 * @author : xinyanfei
 * @date : 2025-08-30 12:14:13
 * Copyright (c) , 京东商城  All Rights Reserved.
 */
public class ModuleEableCondition  implements Condition {

    public static ConditionContext context;
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {

        ModuleEableCondition.context =  context;

        String moduleName = (String) metadata.getAnnotationAttributes(ModuleEnable.class.getName()).get("value");

        if (!StringUtils.hasText(moduleName)) {
            return false;
        }

//        String propertyKey = "module." + moduleName + ".enabled";
        String propertyKey = "app.modules." + moduleName;
        String enabledValue = context.getEnvironment().getProperty(propertyKey);

        // 如果未配置，默认为禁用
        if (!StringUtils.hasText(enabledValue)) {
            return false;
        }
        boolean status = "on".equalsIgnoreCase(enabledValue) ||
                         "true".equalsIgnoreCase(enabledValue) ||
                         "yes".equalsIgnoreCase(enabledValue) ||
                         "1".equals(enabledValue);

        ModuleEnableConst.setOnOff(moduleName,status);

        return status;
    }
}